/*
 * @Author: szx
 * @Date: 2022-01-04 13:07:42
 * @LastEditTime: 2022-01-04 13:24:08
 * @Description:
 * @FilePath: \leetcode\100-199\134\134.js
 */
/**
 * @param {number[]} gas
 * @param {number[]} cost
 * @return {number}
 */
var canCompleteCircuit = function (gas, cost) {
    const length = gas.length;
    let cnt = 0;
    let max = -Infinity;
    let res = -1;
    for (let i = 0; i < length; i++) {
        if (max < -cnt) {
            res = i;
            max = -cnt;
        }
        cnt += gas[i] - cost[i];
    }
    if (cnt < 0) return -1;
    return res;
};
console.log(canCompleteCircuit([2,3,4], [3,4,3]));
